package com.jc.order.common;

import java.math.BigDecimal;
import java.util.List;

public class ValidatorUtil {


    public static void checkRequired(List<String> errors, Object value, String fieldName) {
        if (value == null ||
                (value instanceof String && ((String) value).trim().isEmpty()) ||
                (value instanceof Number && ((Number) value).doubleValue() == 0)) {
            errors.add(fieldName + "为必填项");
        }
    }

    public static void checkMaxLength(List<String> errors, String value, int maxLength, String fieldName) {
        if (value != null && value.length() > maxLength) {
            errors.add(fieldName + "长度超过限制（最大长度：" + maxLength + "）");
        }
    }


    // 添加枚举值校验方法
    public static void checkEnumValue(List<String> errors, String value,
                                      List<String> validValues, String fieldName) {
        if (value != null && !validValues.contains(value)) {
            errors.add(fieldName + "值无效，有效值: " + validValues);
        }
    }

    // 添加金额比较方法
    public static void checkAmountNotExceed(List<String> errors,
                                            BigDecimal amount,
                                            BigDecimal maxAmount,
                                            String fieldName) {
        if (amount != null && maxAmount != null &&
                amount.compareTo(maxAmount) > 0) {
            errors.add(fieldName + "不能大于" + maxAmount);
        }
    }


}
